{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "from huggingface_hub import login\n",
    "login()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from agentlego.apis import load_tool\n",
    "tools = [\n",
    "    load_tool(tool_type, device='cuda').to_transformers_agent()\n",
    "    for tool_type in ['ImageCaption', 'TextToSpeech']\n",
    "]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from transformers import HfAgent\n",
    "from pathlib import Path\n",
    "chat_prompt_template = Path('hf_demo_prompts.txt').read_text()\n",
    "agent = HfAgent(\"https://api-inference.huggingface.co/models/bigcode/starcoder\", additional_tools=tools, chat_prompt_template=chat_prompt_template)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Remove the huggingface tools for the demo.\n",
    "for k in list(agent.toolbox.keys()):\n",
    "    if agent.toolbox[k] not in tools:\n",
    "        agent.toolbox.pop(k)\n",
    "print(agent.toolbox.keys())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from PIL import Image\n",
    "image = Image.open(requests.get('http://download.openmmlab.com/agentlego/human.jpg', stream=True).raw)\n",
    "agent.chat('Please tell me the description of the image', image=image)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "agent.chat('Please speak the above description into audio')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.3"
  },
  "vscode": {
   "interpreter": {
    "hash": "bda9b52cc8d22ac423ba9b9f5be79e8e2fd504b1f615702a53c6fbd8a8b30b94"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
